﻿namespace SilverSea.Databases
{
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Threading;

    public class SqlServerConnection : IConnection
    {
        private bool bool_0;
        private int int_0;
        private SqlConnection sqlConnection_0;
        private string string_0;
        private string string_1;
        private string string_2;
        private string string_3;

        public event DatabaseConnectionErrorEventHandler DatabaseConnectionErrorEvent;

        public SqlServerConnection()
        {
            this.string_0 = string.Empty;
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.sqlConnection_0 = new SqlConnection();
        }

        public SqlServerConnection(string serverName, string databaseName)
        {
            this.string_0 = string.Empty;
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.sqlConnection_0 = new SqlConnection();
            this.string_0 = serverName;
            this.string_1 = databaseName;
        }

        public SqlServerConnection(string serverName, string databaseName, string userName, string password)
        {
            this.string_0 = string.Empty;
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.sqlConnection_0 = new SqlConnection();
            this.string_0 = serverName;
            this.string_1 = databaseName;
            this.string_2 = userName;
            this.string_3 = password;
        }

        public SqlServerConnection(string serverName, string databaseName, string userName, string password, bool enableMARS)
        {
            this.string_0 = string.Empty;
            this.string_1 = string.Empty;
            this.string_2 = "sa";
            this.string_3 = string.Empty;
            this.sqlConnection_0 = new SqlConnection();
            this.string_0 = serverName;
            this.string_1 = databaseName;
            this.string_2 = userName;
            this.string_3 = password;
            this.bool_0 = enableMARS;
        }

        public void Close()
        {
            if (this.sqlConnection_0.State != ConnectionState.Closed)
            {
                this.sqlConnection_0.Close();
                this.sqlConnection_0.Dispose();
            }
        }

        public bool ExecuteCommand(string commandString)
        {
            SqlCommand command = new SqlCommand(commandString, this.sqlConnection_0);
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteCommand(string commandString, string parameters, byte[] values)
        {
            SqlCommand command = new SqlCommand(commandString, this.sqlConnection_0);
            command.Parameters.Add(parameters, SqlDbType.Image);
            if (values != null)
            {
                command.Parameters[parameters].Value = values;
            }
            else
            {
                command.Parameters[parameters].Value = DBNull.Value;
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteCommand(string commandString, string[] parameters, string[] values, SqlDbType[] sqlDbType)
        {
            SqlCommand command = new SqlCommand {
                CommandText = commandString,
                Connection = this.sqlConnection_0,
                CommandType = CommandType.Text
            };
            SqlParameter parameter = null;
            for (int i = 0; i < parameters.Length; i++)
            {
                parameter = new SqlParameter {
                    ParameterName = parameters[i],
                    SqlValue = values[i],
                    SqlDbType = sqlDbType[i]
                };
                command.Parameters.Add(parameter);
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteCommand(string commandString, string parameter1, byte[] value1, string parameter2, byte[] value2)
        {
            SqlCommand command = new SqlCommand(commandString, this.sqlConnection_0);
            command.Parameters.Add(parameter1, SqlDbType.Image);
            if (value1 != null)
            {
                command.Parameters[parameter1].Value = value1;
            }
            else
            {
                command.Parameters[parameter1].Value = DBNull.Value;
            }
            command.Parameters.Add(parameter2, SqlDbType.Image);
            if (value2 != null)
            {
                command.Parameters[parameter2].Value = value2;
            }
            else
            {
                command.Parameters[parameter2].Value = DBNull.Value;
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute command: " + exception.Message + "\n" + commandString);
                }
                return false;
            }
        }

        public bool ExecuteStoredProcedure(string storedProcedureName, string[] parameters, string[] values, SqlDbType[] sqlDbType)
        {
            SqlCommand command = new SqlCommand {
                CommandText = storedProcedureName,
                Connection = this.sqlConnection_0,
                CommandType = CommandType.StoredProcedure
            };
            SqlParameter parameter = null;
            for (int i = 0; i < parameters.Length; i++)
            {
                parameter = new SqlParameter {
                    ParameterName = parameters[i],
                    SqlValue = values[i],
                    SqlDbType = sqlDbType[i]
                };
                command.Parameters.Add(parameter);
            }
            try
            {
                command.ExecuteNonQuery();
                return true;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while excute store procedure: " + exception.Message + "\n" + storedProcedureName);
                }
                return false;
            }
        }

        public string GetConnectionString()
        {
            string str = string.Empty;
            if (this.int_0 == 0)
            {
                str = "server=" + this.string_0 + ";database=" + this.string_1 + ";Integrated Security=true";
            }
            else
            {
                str = "server=" + this.string_0 + ";database=" + this.string_1 + ";uid=" + this.string_2 + ";pwd=" + this.string_3;
            }
            if (this.bool_0)
            {
                str = str + ";MultipleActiveResultSets=true";
            }
            return str;
        }

        public DataSet GetDataSet(string commandString)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(commandString, this.sqlConnection_0);
            DataSet dataSet = new DataSet();
            try
            {
                adapter.Fill(dataSet);
                adapter.Dispose();
                return dataSet;
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while get dataSet fom table: " + exception.Message + "\n" + commandString);
                }
            }
            return null;
        }

        public SqlDataReader GetReader(string commandString)
        {
            SqlCommand command = new SqlCommand(commandString, this.sqlConnection_0);
            return command.ExecuteReader();
        }

        public DataTable GetTable(string commandString)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(commandString, this.sqlConnection_0);
            DataSet dataSet = new DataSet();
            try
            {
                adapter.Fill(dataSet);
                adapter.Dispose();
                return dataSet.Tables[0];
            }
            catch (Exception exception)
            {
                if (this.DatabaseConnectionErrorEvent != null)
                {
                    this.DatabaseConnectionErrorEvent("Error while get data fom table: " + exception.Message + "\n" + commandString);
                }
            }
            return null;
        }

        public bool Open()
        {
            try
            {
                if (this.sqlConnection_0.State == ConnectionState.Open)
                {
                    this.sqlConnection_0.Close();
                }
                this.sqlConnection_0.ConnectionString = this.GetConnectionString();
                this.sqlConnection_0.Open();
                return true;
            }
            catch
            {
                return false;
            }
        }

        public int Authentication
        {
            get
            {
                return this.int_0;
            }
            set
            {
                this.int_0 = value;
            }
        }

        public string DatabaseName
        {
            get
            {
                return this.string_1;
            }
            set
            {
                this.string_1 = value;
            }
        }

        public bool EnableMARS
        {
            get
            {
                return this.bool_0;
            }
            set
            {
                this.bool_0 = value;
            }
        }

        public string Password
        {
            get
            {
                return this.string_3;
            }
            set
            {
                this.string_3 = value;
            }
        }

        public string ServerName
        {
            get
            {
                return this.string_0;
            }
            set
            {
                this.string_0 = value;
            }
        }

        public ConnectionState State
        {
            get
            {
                return this.sqlConnection_0.State;
            }
        }

        public string UserName
        {
            get
            {
                return this.string_2;
            }
            set
            {
                this.string_2 = value;
            }
        }
    }
}

